<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="报价标题" prop="title">
				<el-input v-model="form.title" placeholder="报价标题" clearable maxlength="80"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="报价单号" prop="quotationCode">
						<el-input v-model="form.quotationCode" placeholder="报价单号" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="关联询价单" prop="inquiryId">
						<el-select
							v-model="form.inquiryId"
							filterable
							remote
							reserve-keyword
							placeholder="请输入关键词"
							:remote-method="remoteMethod"
							:loading="loading"
							style="width:100%"
							@change="getCrmInquiryFormJsonById"
						>
							<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-divider content-position="center">报价明细</el-divider>
			<sc-form-table v-model="mx" :addTemplate="addTemplate" drag-sort placeholder="暂无数据">
				<el-table-column prop="productId" label="产品名称" width="200">
					<template #default="scope">
						<el-select
							v-model="scope.row.productId"
							filterable
							remote
							reserve-keyword
							placeholder="请输入关键词"
							:remote-method="remoteProductMethod"
							:loading="loading"
							style="width:100%"
							@change="setOtherValue(scope.row, scope.$index)"
						>
							<el-option v-for="item in productOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</template>
				</el-table-column>
				<el-table-column prop="materialCode" label="产品编码" width="150" :show-overflow-tooltip="true">
					<template #default="scope">
						{{scope.row.materialCode}}
					</template>
				</el-table-column>
				<el-table-column prop="model" label="产品型号" width="100" align="center" :show-overflow-tooltip="true">
					<template #default="scope">
						{{scope.row.model}}
					</template>
				</el-table-column>
				<el-table-column prop="delivery" label="交货期" width="150" align="center">
					<template #default="scope">
						<el-date-picker v-model="scope.row.delivery" placeholder="交货期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</template>
				</el-table-column>
				<el-table-column prop="count" label="数量" width="100">
					<template #default="scope">
						<el-input-number v-model="scope.row.count" clearable controls-position="right" style="width: 80px;" @change="setTotal(scope.row)"></el-input-number>
					</template>
				</el-table-column>
				<el-table-column prop="unit" label="单位" width="100" align="center">
					<template #default="scope">
						<sc-select v-model="scope.row.unit" :params="{code:'crmUnit'}" :apiObj="$API.crm.crmDic.getCrmDicByCode" clearable filterable style="width: 100%"></sc-select>
					</template>
				</el-table-column>
				<el-table-column prop="guidePrice" label="单价" width="150" align="center">
					<template #default="scope">
						<el-input-number v-model="scope.row.guidePrice" placeholder="单价" clearable style="width: 130px;" @change="setTotal(scope.row)" controls-position="right"></el-input-number>
					</template>
				</el-table-column>
				<el-table-column prop="total" label="小计" width="100" align="center">
					<template #default="scope">
						{{scope.row.total}}
					</template>
				</el-table-column>
				<el-table-column prop="remark" label="备注" align="center">
					<template #default="scope">
						<el-input v-model="scope.row.remark" clearable></el-input>
					</template>
				</el-table-column>
			</sc-form-table>
			<el-row :gutter="24" style="margin-top: 10px;">
				<el-col :lg="12">
					<el-form-item label="税费" prop="tax">
						<el-input-number v-model="form.tax" placeholder="税费" clearable></el-input-number>/元
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="总价" prop="totalPrice">
						<el-input-number v-model="form.totalPrice" placeholder="总价" clearable></el-input-number>/元
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="截止日期" prop="closeTime">
						<el-date-picker v-model="form.closeTime" placeholder="截止日期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="审批人" prop="approvedUser">
						<el-select v-model="form.approvedUser" clearable filterable style="width: 100%">
							<el-option v-for="item in userList" :key="item.accountId" :label="item.userName" :value="item.accountId"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="报价备注">
				<el-input v-model="form.remark" placeholder="报价备注" clearable maxlength="380" type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'crm'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import AttachMenu from "@/views/module/attachMenu";

export default {
	emits: ['success', 'closed'],
	components: {
		AttachMenu
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			loading:false,
			addTemplate: {
				productId: '',
				productName:"",
				materialCode: '',
				model: '',
				count: 0,
				unit:"",
				guidePrice:0,
				delivery:"",
				remark: "",
			},
			mode: "add",
			productOptions:[],
			titleMap: {
				add: '创建报价单',
				edit: '编辑报价单',
				show: '查看'
			},
			options:[],
			fileList:[],
			userList:[],
			mx:[],
			apiObj:this.$API.file.attach.uploadFile,
			form: {
				quotationId:'',
				title: '',
				quotationCode: '',
				inquiryId: '',
				tax:'',
				totalPrice:'',
				closeTime:'',
				approvedUser:'',
				remark:'',
				formJson:'',
				attachId:''
			},
			rules: {
				title: [
					{required: true, message: '报价标题不能为空', trigger: 'blur'}
				],
				quotationCode: [
					{required: true, message: '报价单号不能为空', trigger: 'blur'}
				],
				inquiryId:[
					{required: true, message: '关联询价单号不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.getCrmApprovedUserList();
	},
	methods: {
		setTotal(row)
		{
			row.total = row.guidePrice*row.count;
		},
		async getCrmInquiryListForSelect(inquiryId)
		{
			let res = await this.$API.crm.crmInquiry.getCrmInquiryListForSelect.get({inquiryId: inquiryId});
			if (res.code == 200) {
				this.options = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async remoteMethod(keyword) {
			let res = await this.$API.crm.crmInquiry.getCrmInquiryListForSelect.get({keyword: keyword});
			if (res.code == 200) {
				this.options = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.form.formJson = JSON.stringify(this.mx);
					var res;
					if (this.mode == "add") {
						res = await this.$API.crm.crmQuotation.insertCrmQuotation.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.crm.crmQuotation.updateCrmQuotation.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		async setData(data) {
			this.getCrmInquiryListForSelect(data.inquiryId)
			this.form.quotationId = data.quotationId
			this.form.title = data.title
			this.form.quotationCode = data.quotationCode
			this.form.inquiryId = data.inquiryId
			this.form.tax = data.tax
			this.form.totalPrice = data.totalPrice
			this.form.closeTime = data.closeTime
			this.form.approvedUser = data.approvedUser
			this.form.remark = data.remark
			this.form.attachId = data.attachId
			if(data.formJson!=undefined&&data.formJson!='')
			{
				let listMx = JSON.parse(data.formJson);
				let params=[];
				listMx.forEach(item=>{
					params.push(item.productId);
				})
				let res = await this.$API.crm.crmProduct.getCrmProductListByIdsForSelect.get({ids:params});
				if (res.code == 200) {
					this.productOptions = res.data;
					this.mx = listMx;
				}
			}
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
		async remoteProductMethod(keyword) {
			let res = await this.$API.crm.crmProduct.getAllProductListForSelect.get({keyword: keyword});
			if (res.code == 200) {
				this.productOptions = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async getAllProductListForSelect(productId){
			let res = await this.$API.crm.crmProduct.getAllProductListForSelect.get({productId: productId});
			if (res.code == 200) {
				this.productOptions = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		setOtherValue(row,index)
		{
			this.productOptions.forEach(item=>{
				if(item.value==row.productId)
				{
					row.materialCode=item.materialCode;
					row.productName = item.label;
					row.model = item.model;
					row.guidePrice = item.guidePrice;
				}
			})
		},
		async getCrmApprovedUserList()
		{
			let res = await this.$API.crm.crmRole.getCrmApprovedUserList.get();
			if (res.code == 200) {
				this.userList = res.data;
			}
		},
		async getCrmInquiryFormJsonById(val)
		{
			let res = await this.$API.crm.crmInquiry.getCrmInquiryFormJsonById.get({inquiryId: val});
			if (res.code == 200) {
				let listMx = JSON.parse(res.data.formJson);
				let params=[];
				listMx.forEach(item=>{
					params.push(item.productId);
					item.total = item.guidePrice*item.count;
				})
				res = await this.$API.crm.crmProduct.getCrmProductListByIdsForSelect.get({ids:params});
				if (res.code == 200) {
					for(let i=0;i<res.data.length;i++)
					this.productOptions = res.data;
					this.mx = listMx;
				}
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
